package repository

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"mts/log_srv/internal/domain"
	"mts/log_srv/internal/repository/dao"
	"strconv"
)

type LangRepo interface {
	FindLangByUserId(ctx *gin.Context, id string) (string, error)
	UpdateLangByUserId(ctx *gin.Context, lan domain.Language) error
}
type GormLangRepository struct {
	langDao dao.LangDao
}

func (g GormLangRepository) UpdateLangByUserId(ctx *gin.Context, lan domain.Language) error {
	if err := g.langDao.UpdateLangByUserId(ctx, g.toEntity(lan)); err != nil {
		return err
	}
	return nil
}

func NewLangRepo(langDao dao.LangDao) LangRepo {
	return &GormLangRepository{langDao: langDao}
}

func (g GormLangRepository) FindLangByUserId(ctx *gin.Context, id string) (string, error) {
	langId, err := g.langDao.FindLangByUserId(ctx, id)
	if err != nil {
		zap.S().Errorf("获取日志失败:%s", err)
		return "", err
	}
	return langId, err
}

func (g GormLangRepository) toEntity(d domain.Language) dao.Language {
	log := dao.Language{
		ID:     d.ID,
		UserId: strconv.Itoa(d.UserId),
		LangID: d.LangID,
	}
	return log
}
